import { reomveUserData, userToken } from "./account";

const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL + '/api';
const fetch = (method: 'GET' | 'POST', url: string, data = {}, option = {}) => {


  const { loading = false, toast = false, ctoken = null, header } = option as any;
  return new Promise((resolve, reject) => {
    if (loading) {
      uni.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    uni.request({
      url: BASE_URL + url,
      data,
      method,
      header: {
        'content-type': 'application/json',
        'Authorization': userToken(),
        ...header
      },
      success: (res) => {
        // console.log("网络请求🏅️🏅️🏅️🏅️🏅️🏅️🏅️ \n： ", res);
        const { statusCode } = res
        uni.hideLoading()
        if (statusCode == 200) {
          const { code, message, error, status, data } = res.data as { code: number, message: string, error: string, status: string, data: any }
          if (code == 200001) {
            resolve(data)
            if (toast) {
              uni.showToast({
                title: message,
                icon: "none"
              })
            }
          } else if (code === 401001) {
            uni.showToast({
              title: "登录失效请重新登录",
              icon: "none"
            })
            reomveUserData(true)
          } else {
            reject(res.data)
            uni.showToast({
              title: message,
              icon: "none"
            })
          }
        } else {
          reject(res)
          uni.showToast({
            title: "请求错误",
            icon: "none"
          })
        }
      },
      fail: (error) => {
        // console.log("网络请求❌❌❌❌❌❌\n ", errMsg);
        uni.hideLoading()
        const { errMsg } = error
        let msg = '请求出错，稍后重试'
        if (errMsg.includes('timeout')) {
          msg = '请求超时，稍后重试'
        }
        uni.showToast({
          title: msg,
          icon: "none"
        })
        reject(error)

      }
    })
  })
}
type Option = {
  /** 请求是否显示loading */
  loading: boolean;
  /** 请求错误提示是否显示 */
  toast: boolean;
  /** 自定义token */
  ctoken: string;
  /**  */
  header: any;
}
/**
 * GET请求
 */
export function Get(url: string, data: any, option: Option) {
  return fetch('GET', url, data, option)
}
/**
 * POST请求
 */
export function Post(url: string, data: any, option: Option) {
  return fetch('POST', url, data, option)
}

export function loadPictrue(filePath: any, reform_id: number, remark: string) {
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: '上传中...',
      mask: true
    })
    uni.uploadFile({
      url: BASE_URL + '/v1/applet/reforms/reformProblem',
      filePath: filePath,
      name: 'photo',
      formData: {
        'reform_id': reform_id,
        'remark': remark
      }, header: {
        'content-type': 'application/json',
        'Authorization': userToken()
      },
      success: (uploadFileRes) => {
        uni.hideLoading()
        let { data } = uploadFileRes as { data: string }
        let { code, message } = JSON.parse(data) as { code: number, message: string }
        if (code === 200001) {
          resolve({
            code: 0,
            message: "上传成功"
          })
        } else {
          uni.showToast({
            title: message,
            icon: "none"
          })
          reject({
            code: code,
            message: message
          })
        }
      },
      fail: (fail) => {
        uni.hideLoading()
        uni.showToast({
          title: "上传图片失败",
          icon: "none"
        })
        reject({
          code: -1,
          message: "上传图片失败"
        })
      },
    })
  })
}
